package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ArticleDAO;
import entity.Article;

/**
 * Servlet implementation class ShowThemeServlet
 */
@WebServlet("/ShowThemeServlet")
public class ShowThemeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ArticleDAO articleDAO = ArticleDAO.getInstance();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowThemeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
//		response.setContentType("text/html;charset=UTF-8");
//		response.setCharacterEncoding("utf-8");
		
		 try{
				int theme_id = Integer.parseInt(request.getParameter("theme_id"));
				
				//得到属于该主题的帖子列表
				List<Article> articles = articleDAO.list_top(theme_id);
				List<Article> articles2 = articleDAO.list(theme_id);
				for (Article a : articles2) {
					articles.add(a);
				}
				request.setAttribute("articles", articles);
				
				request.getRequestDispatcher("list.jsp").forward(request, response);
				
			}catch(Exception e){
				  e.printStackTrace();
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
